package cn.minsh.lims.eoms.modules.app.controller;

import java.util.Arrays;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.minsh.lims.eoms.modules.app.entity.PlanAgentEntity;
import cn.minsh.lims.eoms.modules.app.service.PlanAgentService;
import cn.minsh.lims.eoms.common.utils.PageUtils;
import cn.minsh.lims.eoms.common.utils.R;
import cn.minsh.lims.eoms.common.utils.ParamUtils;
import cn.minsh.lims.eoms.modules.app.utils.UserUtils;
import javax.servlet.http.HttpServletRequest ;
import org.springframework.validation.BindingResult;
import javax.validation.Valid;

/**
 * 巡检计划经办人
 *
 * @author lht
 * @email sunlightcs@gmail.com
 * @date 2021-06-24 16:04:19
 */
@RestController
@RequestMapping("app/planagent")
//@Api(tags="巡检计划经办人")
public class PlanAgentController {
    @Autowired
    private PlanAgentService planAgentService;

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params){
        try {
            PageUtils page = planAgentService.queryPage(params);

           return R.ok().put("page", page);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e) ;
        }
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("详情")
    public R info(@PathVariable("id") Long id){

        try {
            PlanAgentEntity planAgent = planAgentService.getById(id);
            return R.ok().put("planAgent", planAgent);
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e) ;
        }
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加")
    public R save(@RequestBody @Valid PlanAgentEntity planAgent,BindingResult bindingResult,HttpServletRequest req){

        try {
            ParamUtils.check(bindingResult);
            planAgentService.add(planAgent,UserUtils.getUserId(req));
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e) ;
        }
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("修改")
    public R update(@RequestBody @Valid PlanAgentEntity planAgent,BindingResult bindingResult,HttpServletRequest req){
        try {
          ParamUtils.check(bindingResult);
          planAgentService.updateBy(planAgent,UserUtils.getUserId(req));
          return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e) ;
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public R delete(@RequestBody Long[] ids,HttpServletRequest req){
        try {
            planAgentService.removeByIds(Arrays.asList(ids));
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e) ;
        }
    }

}
